const { Menu, Role, User, getUserRole, getUserRoleMenu } = require('../models/role')
module.exports = {
    async getMenu(req, res) {
        // 角色所含有的权限
        const data = await getUserRoleMenu(1)
        res.json({
            code: 200,
            data: data
        })

    },
    test(req, res) {
        res.json({
            code: 200,
            data: '测试成功'
        })
    },

    // 查看所有menu
    async getAllMenu(req, res) {
        const data = await Menu.findAll()
        res.json({
            code: 200,
            data: data
        })
    },
    // 创建menu
    async createMenu(req, res) {
        let { name, menu_code, parent_id, nodeType, icon, link_url, level } = req.body

        // 判断有无parent_id
        if (parent_id) {
            const parent = await Menu.findOne({
                where: {
                    id: parent_id
                }
            })
            if (!parent) {
                res.json({
                    code: 400,
                    message: '父级菜单不存在'
                })
                return
            }
            level = parent.level + 1
        }
        // 判断有无缺失值
        if (!name || !menu_code || !nodeType) {
            res.json({
                code: 400,
                message: '缺少参数'
            })
            return
        }
        if (!level) {
            level = 1
        }
        const data = await Menu.create({
            name: name,
            menu_code: menu_code,
            parent_id: parent_id,
            nodeType: nodeType,
            icon: icon,
            link_url: link_url,
            level: level
        })
        res.json({
            code: 200,
            data: data
        })
        return
    },
    // 修改menu
    async updateMenu(req, res) {
        const { id } = req.params
        const { name, menu_code, parent_id, nodeType, icon, link_url, level } = req.body
        const data = await Menu.update({
            name: name,
            menu_code: menu_code,
            parent_id: parent_id,
            nodeType: nodeType,
            icon: icon,
            link_url: link_url,
            level: level
        }, {
            where: {
                id: id
            }
        })
        res.json({
            code: 200,
            data: data
        })
    },
    // 删除menu
    async deleteMenu(req, res) {
        const { id } = req.params
        const data = await Menu.destroy({
            where: {
                id: id
            }
        })
        res.json({
            code: 200,
            data: data
        })
    },
    // 查询所有角色
    async getAllRole(req, res) {
        const data = await Role.findAll({
            include: Menu
        })
        res.json({
            code: 200,
            data: data
        })
    },
    // 创建角色
    async createRole(req, res) {
        const { name, menuIdList } = req.body
        if (!name) {
            res.json({
                code: 400,
                message: '缺少参数'
            })
            return
        }
        const data = await Role.create({
            name: name
        })
        // 给角色添加权限
        if (menuIdList) {
            await data.setMenus(menuIdList)
        }
        res.json({
            code: 200,
            data: data
        })

    },
    // 修改角色
    async updateRole(req, res) {
        const { id } = req.params
        const { name, menuIdList } = req.body
        const data = await Role.update({
            name: name
        }, {
            where: {
                id: id
            }
        })
        // 给角色添加权限
        if (menuIdList) {
            const role = await Role.findOne({
                where: {
                    id: id
                }
            })
            role.setMenus(menuIdList)
        }
        res.json({
            code: 200,
            data: data
        })
    },
    // 删除角色
    async deleteRole(req, res) {
        const { id } = req.params
        const data = await Role.destroy({
            where: {
                id: id
            }
        })
        res.json({
            code: 200,
            data: data
        })
    },

    // 查询所有用户
    async getAllUser(req, res) {
        const data = await User.findAll({
            include: Role
        })
        res.json({
            code: 200,
            data: data
        })
    },
    // 创建用户
    async createUser(req, res) {
        const { username, password, roleList, name } = req.body
        if (!username || !password) {
            res.json({
                code: 400,
                message: '缺少参数'
            })
            return
        }
        const data = await User.create({
            username: username,
            password: password,
            name: name
        })
        // 给用户添加角色
        if (roleList) {
            await data.setRoles(roleList)
        }
        res.json({
            code: 200,
            data: data
        })
    },
    // 修改用户
    async updateUser(req, res) {
        const { id } = req.params
        const { username, password, roleList } = req.body
        const data = await User.update({
            username: username,
            password: password
        }, {
            where: {
                id: id
            }
        })
        // 给用户添加角色
        if (roleList) {
            const user = await User.findOne({
                where: {
                    id: id
                }
            })
            user.setRoles(roleList)
        }
        res.json({
            code: 200,
            data: data
        })
    },
    // 删除用户
    async deteleUser(req, res) {
        const { id } = req.params
        const data = await User.destroy({
            where: {
                id: id
            }
        })
        res.json({
            code: 200,
            data: data
        })
    },

    // 查询用户所含角色所含权限
    async getUserRole(req, res) {
        const { id } = req.params
        const data = await getUserRole(id)
        res.json({
            code: 200,
            data: data
        })
    },
    // 查询用户所含角色
    async getUserRoleList(req, res) {
        const { id } = req.params
        const user = await User.findByPk(id, {
            include: Role
        })
        res.json({
            code: 200,
            data: user
        })
    },
    // 查询角色所含权限
    async getRoleMenu(req, res) {
        const { id } = req.params
        const role = await Role.findByPk(id, {
            include: Menu
        })
        res.json({
            code: 200,
            data: role
        })
    }

}